<?php
// +----------------------------------------------------------------------
// | HkCms 日志记录
// +----------------------------------------------------------------------
// | Copyright (c) 2012-2019 http://www.hkcms.cn, All rights reserved.
// +----------------------------------------------------------------------
// | Author: 广州恒企教育科技有限公司 <admin@hkcms.cn>
// +----------------------------------------------------------------------

namespace app\admin\controller;

use common\controller\AdminBase;

class Logs extends AdminBase
{
    //后台登陆日志
    public function loginlog() 
    {
        if ( request()->isPost() ) {

            $this->redirect('loginlog', $_POST);
        }

        $where = array();

        $username = input('username');

        $start_time = input('start_time');

        $end_time = input('end_time');

        $loginip = input('loginip');

        $status = input('status');

        if (!empty($username)) {

            $where['username'] = array('like', '%' . $username . '%');
        }

        if (!empty($start_time) && !empty($end_time)) {

            $start_time = strtotime($start_time);

            $end_time = strtotime($end_time) + 86399;

            $where['logintime'] = array(array('GT', $start_time), array('LT', $end_time), 'AND');
        }

        if (!empty($loginip)) {

            $where['loginip '] = array('like', "%{$loginip}%");
        }

        if ($status != '') {

            $where['status'] = $status;
        }

        $model = model("admin/Loginlog");

        $query['m'] = request()->module();
        $query['c'] = request()->controller();
        $query['a'] = request()->action();

        $data = $model->where($where)->order(array('id' => 'DESC'))->paginate('10',false,['query'=>$query]);
        
        $this->assign("data", $data);
        
        $this->assign('where', $where);
        	        
        return $this->fetch();
    }

    //删除一个月前的登陆日志
    public function deleteloginlog() 
    {

        if (model("admin/Loginlog")->deleteAMonthago()) {

            $this->success("删除登陆日志成功！");

        } else {
            
            $this->error("删除登陆日志失败！");
        }
    }

    //操作日志查看
    public function index() {

        if ( request()->isPost() ) {

            $this->redirect('index', $_POST);
        }

        $uid = input('uid');

        $start_time = input('start_time');

        $end_time = input('end_time');

        $ip = input('ip');

        $status = input('status');

        $where = array();

        if (!empty($uid)) {

            $where['uid'] = array('eq', $uid);
        }
        if (!empty($start_time) && !empty($end_time)) {

            $start_time = strtotime($start_time);

            $end_time = strtotime($end_time) + 86399;

            $where['time'] = array(array('GT', $start_time), array('LT', $end_time), 'AND');
        }

        if (!empty($ip)) {

            $where['ip '] = array('like', "%{$ip}%");

        }
        if ($status != '') {

            $where['status'] = (int) $status;
        }

        $count = model("operationlog")->where($where)->count();

        $page = $this->page($count, 20);

        $query['m'] = request()->module();
        $query['c'] = request()->controller();
        $query['a'] = request()->action();

        $Logs = model("operationlog")->where($where)->order(array("id" => "desc"))->paginate('10',false,['query'=>$query]);


        $this->assign("logs", $Logs);

        $aa = compact($uid,$start_time,$end_time,$ip,$status);
        $this->assign("get_data", $aa);

        $this->assign("uid", $uid);
        $this->assign("start_time", date('Y-m-d',$start_time));
        $this->assign("end_time", date('Y-m-d',$end_time));
        $this->assign("ip", $ip);
        $this->assign("status", $status);

        return $this->fetch();
    }

    //删除一个月前的操作日志
    public function deletelog() {
        if (model("admin/Operationlog")->deleteAMonthago()) {
            $this->success("删除操作日志成功！");
        } else {
            $this->error("删除操作日志失败！");
        }
    }
}
